﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using BusStationClient.Properties;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;

namespace BusStationClient
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(DataTable dt)
        {
            InitializeComponent();
            _dtStations = dt;
            _dtPoints = new DataTable();
            _dtPoints.Columns.Add("经度");
            _dtPoints.Columns.Add("纬度");
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Init();
        }


        private GMapOverlay _olayStations;
        private GMapOverlay _overlay;
        private DataTable _dtStations;
        private DataTable _dtPoints;

        private void MainMap_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                PointLatLng point = MainMap.FromLocalToLatLng(e.X, e.Y);
                Bitmap img = Resources.pk4;
                GMarkerGoogle gMarker = new GMarkerGoogle(point, img)
                {
                    ToolTipText = "经度：" + point.Lng + Environment.NewLine +
                    "纬度：" + point.Lat + Environment.NewLine,
                    ToolTipMode = MarkerTooltipMode.OnMouseOver
                };
                _overlay.Markers.Add(gMarker);

                DataRow dr = _dtPoints.NewRow();
                dr["经度"] = point.Lng;
                dr["纬度"] = point.Lat;
                _dtPoints.Rows.Add(dr);
                label_pointCount.Text = _dtPoints.Rows.Count.ToString();
                int markercount = _overlay.Markers.Count;
                if(markercount>=2)
                {
                    PointLatLng prevPoint = _overlay.Markers[markercount - 2].Position;
                    DrawLineBetweenTwoPoint(prevPoint,point);
                }
            }           
        }

        private void Init()
        {
            //Map
            double theCenterLat = 29.244019;
            double theCenterLng = 115.80255;

            MainMap.Manager.Mode = AccessMode.ServerAndCache;
            MainMap.MapProvider = GMapProviders.GoogleChinaMap;
            _overlay = new GMapOverlay("Overlay");
            MainMap.Overlays.Add(_overlay);
            _olayStations = new GMapOverlay("Stations");
            MainMap.Overlays.Add(_olayStations);  
            MainMap.MaxZoom = 18;
            MainMap.MinZoom = 10;

            MainMap.Zoom = 12;
            MainMap.Position = new PointLatLng(theCenterLat, theCenterLng);
            MainMap.DragButton = MouseButtons.Left;

            this.WindowState = FormWindowState.Maximized;

            DrawStation();
        }

        private void DrawLineBetweenTwoPoint(PointLatLng startpoint, PointLatLng endpoint)
        {
            List<PointLatLng> points = new List<PointLatLng>();
            points.Add(startpoint);
            points.Add(endpoint);
            GMapRoute r = new GMapRoute(points, "");
            Pen pen = new Pen(Color.Green, 6);           
            r.Stroke = pen;
            _overlay.Routes.Add(r);
        }

        //画出两点之间的线路
        private void DrawrouteBetweenTowPoint(PointLatLng startpoint, PointLatLng endpoint)
        {
            RoutingProvider rp = MainMap.MapProvider as RoutingProvider;
            if (rp != null)
            {
                rp = GMapProviders.GoogleMap;
            }
            MapRoute route = rp.GetRoute(startpoint, endpoint, false, false, (int)MainMap.Zoom);
            if (route != null)
            {
                GMapRoute r = new GMapRoute(route.Points, route.Name);
                _overlay.Routes.Add(r);
                //   gMap.ZoomAndCenterRoute(r);
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            int count = _overlay.Markers.Count;
            if(count>0)
            {
                _overlay.Markers.Remove(_overlay.Markers[count - 1]);
                if(count>1)
                {
                    _overlay.Routes.Remove(_overlay.Routes[count - 2]);
                }
            }
            if(_dtPoints.Rows.Count>0)
            {
                _dtPoints.Rows.RemoveAt(_dtPoints.Rows.Count-1);
            }
            label_pointCount.Text = _dtPoints.Rows.Count.ToString();
        }


        private void DrawStation()
        {
            int i = 0;
            foreach (DataRow dr in _dtStations.Rows)
            {
                string name = dr["站名"].ToString();
                string strLat = dr["纬度"].ToString();
                string strLng = dr["经度"].ToString();
                double lat = double.Parse(strLat) - 0.002780;
                double lng = double.Parse(strLng) + 0.004999;
                PointLatLng pointLatLng = new PointLatLng(lat, lng);
                Bitmap obj = Resources.p1;
                GMarkerGoogle gMarker = new GMarkerGoogle(pointLatLng, obj)
                {
                    ToolTipText =
                        "站名：" + name +
                        Environment.NewLine
                        + "经度：" + strLng +
                        Environment.NewLine
                        + "纬度：" + strLat +
                        Environment.NewLine
                        + "Index：" + i +
                        Environment.NewLine
                    ,
                    ToolTipMode = MarkerTooltipMode.OnMouseOver,
                    Tag = name
                };
                _olayStations.Markers.Add(gMarker);
                i++;
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            //文本文件(*.txt)|*.txt|所有文件(*.*)|*.*
            saveFileDialog1.Filter = "xml文件(*.xml)|*.xml";
            if(saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                XmlDocument xmldoc = new XmlDocument();
                XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "GB2312", null);
                xmldoc.AppendChild(dec);
                XmlNode xmlNode = xmldoc.CreateElement("PointList");
                foreach (DataRow dr in _dtPoints.Rows)
                {
                   // XmlNode xmlNode2 = xmldoc.CreateElement("Point");
                    XmlElement xmlele = xmldoc.CreateElement("Point");
                    xmlele.SetAttribute("Lat",dr["经度"].ToString());
                    xmlele.SetAttribute("Lng", dr["纬度"].ToString());
                    xmlNode.AppendChild(xmlele);
                }                                
                xmldoc.AppendChild(xmlNode);

                string path = saveFileDialog1.FileName;
                xmldoc.Save(path);
            }
        }


       
    }
}
